﻿using BLToolkit.Data;
using BLToolkit.Data.Sql;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Microsoft.Win32;
using Microsoft.SqlServer.Management.Smo;
using Microsoft.SqlServer.Management.Common;
using System.IO;
using log4net;
using log4net.Config;
using Data_Access_Layer;

namespace CaffeBillard
{
    public partial class FormBackupRestore : DevComponents.DotNetBar.Office2007RibbonForm
    {
        protected static readonly ILog log = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
        private bool type;
        private string name = "";
        public FormBackupRestore(bool type)
        {
            InitializeComponent();
            this.type = type;
        }

        private void FormBackupRestore_Load(object sender, EventArgs e)
        {
            name =
                Employee.Objects.FirstOrDefault(x => x.Id == WorkSession.Current.CurrentAccount.EmpId).Name.ToString();
            if (type)
            {
                gbBackup.Enabled = true;
                gbRestore.Enabled = false;
            }
            else
            {
                gbBackup.Enabled = false;
                gbRestore.Enabled = true;
            }
        }

        /// <summary>
        /// Chọn nơi lưu cơ sở dữ liệu
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnBr_Click(object sender, EventArgs e)
        {
            saveFileDialog1.Filter = "(*.bak)|*.bak";

            if(saveFileDialog1.ShowDialog()==DialogResult.OK)
            {
                txtBackup.Text = saveFileDialog1.FileName;
            }
        }


        /// <summary>
        /// Chọn cơ sở dữ liệu để khôi phục
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnBr1_Click(object sender, EventArgs e)
        {
            openFileDialog1.Filter = "(*.bak)|*.bak";

            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                txtRestore.Text = openFileDialog1.FileName;
            }
        }

        /// <summary>
        /// Sao lưu cơ sở dữ liệu
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnBackup_Click(object sender, EventArgs e)
        {
            try
            {
                XmlConfigurator.Configure();
                log.Info(name + " sao luu du lieu");
                if (txtBackup.Text == "")
                    MessageBox.Show("Hãy chọn nơi lưu");
                else
                {
                    var db = new DbManager("DBConnection");
                    db.SetCommand("BACKUP DATABASE BilliardCaffe TO DISK ='" + txtBackup.Text + "'WITH  NOFORMAT,NOINIT,SKIP,STATS = 10");
                    db.ExecuteNonQuery();
                    MessageBox.Show("Sao lưu hoàn thành, tệp tin của bạn được lưu tại " + saveFileDialog1.FileName);
                }
               
            }

            catch (BLToolkit.Data.Sql.SqlException ex)
            {
                MessageBox.Show("Lỗi: " + ex.Message);

            }
            catch (Exception ex1)
            {
                MessageBox.Show("Lỗi: " + ex1.Message);

            }

        }

        /// <summary>
        /// Khôi phục lại cơ sở dữ liệu
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnRestore_Click(object sender, EventArgs e)
        {
            try
            {
                XmlConfigurator.Configure();
                log.Info(name + " phuc hoi lai du lieu du lieu");
                if (txtRestore.Text == "")
                    MessageBox.Show("Hãy chọn cơ sở dữ liệu muốn phục hồi trước");
                else
                {
                    var db = new DbManager();
                    db.Connection = new SqlConnection(@"data source=.\sqlexpress;database=master;Integrated Security =  true");
                     string sqlRestore = "ALTER DATABASE BilliardCaffe SET SINGLE_USER WITH ROLLBACK IMMEDIATE ";
                    sqlRestore += " USE master RESTORE DATABASE BilliardCaffe FROM DISK = N\'" + txtRestore.Text + "\'";
                    sqlRestore += " WITH  FILE = 1, NOUNLOAD, REPLACE, STATS = 10";
                    db.SetCommand(sqlRestore);
                    db.ExecuteNonQuery();
                    MessageBox.Show("Phục hồi dữ liệu thành công! ");
                }
                
            }
            catch (BLToolkit.Data.Sql.SqlException ex)
            {
                MessageBox.Show(ex.Message, "Restore Database");

            }
            catch (Exception ex)
            {
                MessageBox.Show("Erro: " + ex.Message);
            }
          
        }

        /// <summary>
        /// Thoát khỏi menu sao lưu, phục hồi
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnExit_Click(object sender, EventArgs e)
        {
            this.Close();
            
        }

       

    }
}
